<?php

namespace app\common\service\driver;

use app\common\model\driving\DriverLeave;
use think\Db;
use think\Exception;

/**
 * 司机请假服务类
 **/
class DriverLeaveService
{

    /**
     * 请假
     *
     * @param array $params
     * @param mixed $user 用户信息
     * @return bool
     */
    public static function create($params, $user)
    {
        $params['driver_id'] = $user['id'];
        try {
            DriverLeave::create($params);
        } catch (Exception $e) {
            except($e->getMessage());
        }
        return true;
    }

    /**
     * 列表
     *
     * @param integer $page 页数
     * @param integer $page_size 每页数量
     * @param mixed $user 用户信息
     * @return array
     */
    public static function list($page, $page_size,$user)
    {
        $page_size = $page_size > 0 ? $page_size : config('paginate.list_rows');
        if (!($page >= 1)) $page = 1;
        $offset = ($page - 1) * $page_size;

        $where = [];
        $where['driver_id'] = $user['id'];
        $total = DriverLeave::where($where)->count('id');
        $data = [];
        if (!$total) {
            return compact("total", "data");
        }
        $list = DriverLeave::where($where)
            ->order('id desc')
            ->page($page, $page_size)
            ->select();
        $data = $list;
        return compact("total", "data");
    }

}
